<?php
include_once dirname(__FILE__).'/_config.php';
##@include_once CLS_PTH.'tools/File.class.php';
##@include_once CLS_PTH.'lang/Sentence.class.php';
set_time_limit(0);	// 去年运行时间限制

echo "It's best done in Console, otherwise, it may lead to timeout.<br /><br />\r\n\r\n";
//$js_files = File::getFiles(S_PTH, true, 'js');
$s = microtime(true);

$oSentence = new Sentence();
$langs = cache_read('langs.php');
$librarys = cache_read('librarys.php');
$note_arr = array();
foreach($langs['lang_list'] as $langid => $lang_info){
	$lang_path = LNG_PTH.$lang_info['mark'].'/';
	$note_arr[] = " * @@@{$lang_info['mark']}\t{$lang_info['name']}";
	if(File::createFolder($lang_path)){
		travel($lang_info, $librarys['library_tree'], '', $lang_path);
	}
}
echo implode("\n", $note_arr)."\n";
$e = microtime(true);
$t = $e - $s;
echo "All done. Take time {$t} s. At ".date('Y-m-d H:i:s')."<br />\r\n";

function travel($lang_info, $librarys, $pre, $path){
	global $oSentence, $note_arr;
	if(!empty($pre)){
			$pre = $pre.'_';
	}
	foreach($librarys as $libraryid => $library_info){
		$_pre = $pre.$library_info['mark'];
		$note_arr[] = " * {$_pre}\t{$library_info['name']}";
		if(isset($library_info['librarys']) && is_array($library_info['librarys'])){
			travel($lang_info, $library_info['librarys'], $_pre, $path);
		}else{
			$list = $oSentence->get_all('mark,content', array('langid' => $lang_info['id'], 'libraryid' => $libraryid));
			if(empty($list['list'])){
				$data_php = array('There is no sentence in this module.');
				$data_js = array("empty:'There is no sentence in this module.'");
			}else{
				$data_php = array();
				$data_js = array();
				foreach($list['list'] as $row){
					$data_php[$row['mark']] = $row['content'];
					$data_js[] = "'{$row['mark']}':'{$row['content']}'";
				}
			}
			
			$_file_php = $_pre.'.lang.php';
			cache_write($_file_php, $data_php, $path);
			echo ">>>> {$path}{$_file_php}<br />\r\n<br />\r\n";//ob_flush();flush();
			
			// 清除可能存在的EA缓存
			$key = md5("{$path}{$_file_php}");
			ea_clear($key);
			
			$_file_js = $path.$_pre.'.lang.js';
			$data_js = "var _lang = {\n".(implode(",\n", $data_js))."\n};\n$.extend(_l, _lang);";
			File::write_file($_file_js, $data_js);
			echo ">>>> {$_file_js}<br />\r\n<br />\r\n";//ob_flush();flush();
		}
	}
}



?>
